package com.lac.component.test.singleton;

/**
 * @author xiaokuixiao
 * date2025/4/9 13:32
 */
//饿汉式(静态代码块)
public class SingletonTest02 {
    public static void main(String[] args) {
        // 调用返回都是同个实例对象
        Singleton02 instance1 = Singleton02.getInstance();
        Singleton02 instance2 = Singleton02.getInstance();
//        System.out.println(instance1 == instance2);
    }

}

class Singleton02{
    // 类初始化时就实例化对象
    private static Singleton02 instance;

    static { // 在静态代码块种实例化对象
        instance = new Singleton02();
    }

    // 私有化构造器
    private Singleton02(){

    }

    // 提供公有的静态方法给外部调用，返回实例化对象
    public static Singleton02 getInstance(){
        return instance;
    }
}